package com.jy.adscm.operation.mscyb.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.JqzzMachine;
import com.jy.adscm.operation.mscyb.bean.QueryJqzzMachine;
import com.jy.adscm.operation.mscyb.service.JqzzMachineService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2018-12-04
 */
@RestController
@RequestMapping("/adscm/operation/jqzzMachine")
public class JqzzMachineController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    JqzzMachineService jqzzMachineService;

    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody JqzzMachine jqzzMachine, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        jqzzMachine.setTaskreleaseperson(token.getUser().getId().toString());
        jqzzMachine.setTaskreleaseaddtime(new Date());
        jqzzMachine.setTaskfinishstate(0);
        jqzzMachine.setTaskstate(0);
        jqzzMachine.setIstaskstart("0");
        jqzzMachine.setIstaskbegin("0");
        jqzzMachine.setIstaskend("0");
        jqzzMachine.setIsreviewtask("0");
        jqzzMachine.setReviewresult("-1");
        String insert = jqzzMachineService.insertJqzzMachine(jqzzMachine);
        return new SimpleCoreResponse("200","新增成功",insert);
    }

    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer id)throws AdscmSystemException {
        boolean result = jqzzMachineService.deleteById(id);
        return new  SimpleCoreResponse("200","删除成功",result);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody JqzzMachine jqzzMachine, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Boolean result = jqzzMachineService.updateJqzzMachine(jqzzMachine,token);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("selectjqzzMachine")
    public  SimpleCoreResponse  selectjqzzMachine(@RequestBody QueryJqzzMachine queryJqzzMachine,HttpServletRequest request) throws  AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Page<JqzzMachine> page=new Page<>(queryJqzzMachine.getCurrentPage(),queryJqzzMachine.getPageSize());
        page= jqzzMachineService.selectJqzzMachine(page ,queryJqzzMachine,token);
        return  new SimpleCoreResponse("200","查询成功",page);
    }
    @RequestMapping("selectTaskPerformPerson")
    public  SimpleCoreResponse  selectTaskPerformPerson() throws  AdscmSystemException{
        List<JyUser> users = jqzzMachineService.selectTaskPerformPerson();
        return  new SimpleCoreResponse("200","查询成功",users);
    }
    @RequestMapping("selectMachine")
    public  SimpleCoreResponse  selectMachine() throws  AdscmSystemException{
        List<JyCode> jyCodes = jqzzMachineService.selectMachine();
        return  new SimpleCoreResponse("200","查询成功",jyCodes);
    }
}

